package org.bookstore.dao.impl;
import java.util.List;
import org.bookstore.dao.IBookDAO;
import org.bookstore.model.Book;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BookDAO extends HibernateDaoSupport implements IBookDAO{
	public List getNewBook(){
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		Query query=session.createQuery("from Book b");
		query.setFirstResult(0);
		query.setMaxResults(5);
		List books=query.list();
		session.close();
		return books;
	}
	public List getBookByCatalogidPaging(Integer catalogid,int currentPage,int pageSize){
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		Query query=session.createQuery("from Book b where b.catalog.catalogid=?");
		query.setParameter(0, catalogid);
		int startRow=(currentPage-1)*pageSize;
		query.setFirstResult(startRow); 
		query.setMaxResults(pageSize);
		List books=query.list();
		session.close();
		return books;
}
public int getTotalByCatalog(Integer catalogid){
		List books=this.getHibernateTemplate().find("from Book b where b.catalog.catalogid=?",catalogid);
		return books.size();
	}
public List getRequiredBookByName(String name){
	try{
		List books=this.getHibernateTemplate().
					find("from Book where bookname like '%"+name+"%'");
		return books;
	}catch(Exception e){
		e.printStackTrace();
		return null;
	}
}	

public Book getBookById(Integer bookid){
	Book book=(Book) this.getHibernateTemplate().get(Book.class, bookid);
	return book;
}

}

